import { CardProps, ContainerType } from '@/Util/Constant';
import { CardIconType } from '@/share/commonType';
import * as PIXI from 'pixi.js'
import { CardController } from './cardController';

export class BaseCard { // 只负责纸牌显示，不负责事件交互
    protected cardContainer = new PIXI.Container()
    protected cardNum: number;
    protected cardIcon: CardIconType;
    protected controller: CardController
    protected container: ContainerType;
    protected posX: number;
    protected posY: number;
    protected isLandCard: boolean; // 是否是地主牌
    protected scaleSize: number = 1; // 是否是地主牌
    constructor(props: CardProps) {
        this.container = props.container
        this.cardNum = props.cardNum //牌号
        this.cardIcon = props.cardIcon
        this.controller = props.controller
        this.posX = props.posX
        this.posY = props.posY
        this.isLandCard = props.isLandCard
        this.createCard()
        this.scaleSize = props.scale
        props.scale && this.cardContainer.scale.set(props.scale, props.scale)
    }
    removeChild() {
        this.container.removeChild(this.cardContainer)
    }
    protected createCard() {
        // 渲染容器
        let rect = new PIXI.Graphics()
        rect.lineStyle(1, '#aaa')
        rect.beginFill('#fff')
        rect.drawRoundedRect(0, 0, 135, 195, 15)
        rect.endFill()
        // 渲染mask
        let mask = new PIXI.Graphics()
        mask.lineStyle(1, '#aaa')
        mask.beginFill('#fff')
        mask.drawRoundedRect(0, 0, 135, 195, 15)
        mask.endFill()
        this.cardContainer.mask = mask;
        this.cardContainer.addChild(mask)
        this.cardContainer.addChild(rect)
        if (this.cardNum >= 14) {
            this.createJoker()
        } else {
            this.createNormalCard(rect)
        }
        // 创建地主图标
        if (this.isLandCard) {
            const landloadbox = new PIXI.Graphics()
            landloadbox.lineStyle(1, 'black')
            landloadbox.beginFill('#c52647')
            landloadbox.drawRect(0, 0, 200, 30)
            landloadbox.endFill()
            const landloadText = new PIXI.Text('地主', {
                fontFamily: "黑体",
                fill: '#f8e07c',
                fontSize: 20,
                fontWeight: 'bold',
                align: "center",
            }) as PIXI.Text;
            landloadText.x = 75
            landloadText.y = 5
            landloadbox.addChild(landloadText)
            landloadbox.x = 50
            landloadbox.y = 0
            landloadbox.transform.position.y = -50
            landloadbox.transform.rotation = (Math.PI / 180) * 45
            this.cardContainer.addChild(landloadbox)
        }
        this.cardContainer.x = this.posX || 0;
        this.cardContainer.y = this.posY || 0;
        this.container.addChild(this.cardContainer)
    }

    protected createJoker() {
        // 创建纸牌基地
        let cardColor = this.cardNum === 14 ? 'black' : 'red'
        let jokerText = ['J', 'O', 'K', 'E', 'R']
        // 遍历绘制文字
        jokerText.forEach((text, index) => {
            const cardNum = new PIXI.Text(text, {
                fontFamily: "仿宋",
                fill: cardColor,
                fontSize: 30,
                align: "center",
                fontWeight: '600',
            }) as PIXI.Text;
            cardNum.x = 15
            cardNum.y = 15 + 30 * index
            this.cardContainer.addChild(cardNum)
        })

    }
    protected createNormalCard(rect: any) {
        let cardString = this.getCardNumString(this.cardNum)
        let { iconString, iconColor } = this.getCardIconString(this.cardIcon)
        const cardNum = new PIXI.Text(cardString, {
            fontFamily: "仿宋",
            fill: iconColor,
            fontSize: 30,
            align: "center",
            fontWeight: '600',
        }) as PIXI.Text;
        cardNum.x = cardString === '10' ? 7.5 : 15
        cardNum.y = 15
        const cardBigIcon = new PIXI.Text(iconString, {
            fontFamily: "仿宋",
            fill: iconColor,
            fontSize: 135,
            align: "center",
        }) as PIXI.Text;
        cardBigIcon.x = 37.5
        cardBigIcon.y = 82.5
        const cardLitterIcon = new PIXI.Text(iconString, {
            fontFamily: "仿宋",
            fill: iconColor,
            fontSize: 45,
            align: "center",
        }) as PIXI.Text;
        cardLitterIcon.x = 7.5
        cardLitterIcon.y = 52.5
        this.cardContainer.addChild(cardNum)
        this.cardContainer.addChild(cardLitterIcon)
        this.cardContainer.addChild(cardBigIcon)
    }

    getCardId() {
        return `${this.cardNum}-${this.cardIcon}`
    }

    protected getCardIconString(iconNum: CardIconType) {
        let iconString = ''
        let iconColor = 'red'
        switch (iconNum) {
            case CardIconType.BlackHeart:
                iconString = '♠'
                iconColor = 'black'
                break;
            case CardIconType.PlumBlossom:
                iconString = '♣'
                iconColor = 'black'
                break;
            case CardIconType.RedHeart:
                iconString = '♥'
                break;
            case CardIconType.Square:
                iconString = '♦'
                break;
            default:
                break;
        }
        return { iconString, iconColor }
    }
    protected getCardNumString(cardNum: number) {
        let res = ''
        switch (cardNum) {
            case 13:
                res = '2'
                break;
            case 12:
                res = 'A'
                break;
            case 11:
                res = 'K'
                break;
            case 10:
                res = 'Q'
                break;
            case 9:
                res = 'J'
                break;
            default:
                res = (cardNum += 2).toString()
                break;
        }
        return res
    }
}